import {defineStore} from 'pinia';
import {Http, Cache, Helper, Msg} from 'core';

/**
 * 账单
 * @type {{id: string, state(): {companyConfig: {}}, getters: {}, actions: {getConfig(): Promise<undefined|*>}}}
 */
const store = {
    id: "bill",
    state() {
        return {
            pageNo: 1,
            pages: 0,
            records: [],
            pageSize: 10,
            total: 0,
            condition:{},
            hasMore: true,

            billInfo: {},
            billStat: {total: 0, totalAmount: 0, unpaidAmount: 0, paidAmount: 0, overdueAmount: 0},

            orderBillList: [],
        }
    },

    getters: {

    },

    actions: {
        /**
         * 账单统计
         * @param beginTime
         * @param endTime
         * @returns {Promise<*|{totalAmount: number, total: number, overdueAmount: number, unpaidAmount: number, paidAmount: number}>}
         */
        async stat(beginTime, endTime, storeId) {
            Msg.loading("加载中...")
            this.billStat = {total: 0, totalAmount: 0, unpaidAmount: 0, paidAmount: 0, overdueAmount: 0};
            let params = {};
            if(! Helper.empty(beginTime)) {
                params.beginTime = beginTime;
            }

            if(! Helper.empty(endTime)) {
                params.endTime = endTime;
            }

            if(! Helper.empty(storeId)) {
                params.storeId = storeId;
            }

            let {code, data} = await Http.post("/bill/business/statistics", params);
            Msg.loading(false);

            if(code == 200) {
                this.billStat = data;
            }

            return this.billStat;
        },

        /**
         * 查询我的订单列表
         * @param keyword 手机号
         * @param orderState
         * @param startTime
         * @param endTime
         * @returns {Promise<void>}
         */
        async getList(pageNo, pageSize, condition) {
            if (pageNo == 1) {
                this.records = [];
                this.hasMore = true;
            }

            if(! this.hasMore) {
                return;
            }

            // this.condition = condition;
            let params = {current: pageNo, size: pageSize};
            if(! Helper.emptyJson(condition)) {
                params = Object.assign(params, condition);
            }

            Msg.loading("加载中...");
            let {code, data} = await Http.post("/bill/business/page", params);
            Msg.loading(false);

            // Log.info("获取我的订单列表", code, data);
            if (code == 200) {
                this.pages = data.pages;
                this.pageNo = data.current;
                this.pageSize = data.size;
                this.total = data.total;
                if(Helper.emptyArray(data.records)) {
                    this.hasMore = false;
                }
                this.records = this.records.concat(data.records);
            }

            return this.records;
        },

        /**
         * 根据订单编号查询账单信息
         * @param orderNo
         * @returns {Promise<void>}
         */
        async getListByOrderNo(orderNo) {
            Msg.loading("加载中...")
            this.orderBillList = [];
            let {code, data} = await Http.get(`/bill/user/list/${orderNo}`);
            Msg.loading(false);

            if(code == 200) {
                this.orderBillList = data;
            }
            return this.orderBillList;
        },

        /**
         * 查询账单详情
         * @param billId
         */
        async getInfo(billId) {
            Msg.loading("加载中...");
            this.billInfo = {};
            let {code, data} = await Http.get("/bill/business/info/" + billId);
            Msg.loading(false);
            if(code == 200) {
                data.billId = billId;
                this.billInfo = data;
            }

            return this.billInfo;
        },

        /**
         * 线下支付
         * @param billId
         * @param tradeChannel PAY_WAY
         * @param amount
         * @param remark
         */
        async offLinePay(billId, tradeChannel, amount, remark) {
            Msg.loading("加载中...");
            let {code, data} = await Http.post("/bill/business/costReduce", {billId, tradeChannel, amount, remark, fundChangeProject: "OFFLINE_PAY"})
            Msg.loading(false)
            if(code == 200) {
                Msg.success("操作成功");
                return true;
            }
            return false;
        },

        /**
         * 减免
         * @param billId
         * @param amount
         * @returns {Promise<boolean>}
         */
        async eduction(billId, amount) {
            Msg.loading("加载中...");
            let {code, data} = await Http.post("/bill/business/costReduce", {billId, amount, fundChangeProject: "COST_REDUCE"})
            Msg.loading(false)
            if(code == 200) {
                Msg.success("操作成功");
                return true;
            }
            return false;
        },

        async overduePayment(billId) {
            Msg.loading("加载中...");
            let {code, data} = await Http.get("/bill/business/overduePayment/request/" + billId);
            Msg.loading(false)
            if(code == 200) {
                Msg.success("操作成功");
                return true;
            }
            return false;

        }
    }
}

export default defineStore(store)
